package com.jxs.android.sxyw;

import java.io.IOException;
import java.util.Date;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.EomsInfo;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.sxyw.weiget.WheelDateTimeDialog;
import com.jxs.android.util.StartHelper;
import com.jxs.android.util.StringUtils;

public class EomsPowerProcess extends AbstractActivity implements OnClickListener {

	private final int REQ_HITCH_TIME = 0x09906;

	private TextView process_time, tip_label;// 采取处理措施时间

	private LinearLayout process_time_ll;//
	private EomsInfo eomsInfo;
	private String tip = "";

	protected void onCreate(android.os.Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.eoms_power_process);

		if (null == SelfApplication.getUserInfo() || StringUtils.isEmpety(SelfApplication.getUserInfo().getTicket())) {
			PopMsg.show("登录已过期,请重新登录");
			StartHelper.startActivity(Login.class);
			return;
		}

		process_time_ll = (LinearLayout) findViewById(R.id.process_time_ll);
		process_time = (TextView) findViewById(R.id.process_time);
		tip_label = (TextView) findViewById(R.id.tip_label);
		process_time_ll.setOnClickListener(this);

		if (null != getIntent() && null != getIntent().getExtras()) {
			eomsInfo = (EomsInfo) getIntent().getExtras().get("eomsInfo");
			if ("2".equals(eomsInfo.getStatus().trim()) || "3".equals(eomsInfo.getStatus().trim())) {
				tip = "发电时间";
			} else if ("6".equals(eomsInfo.getStatus().trim())) {
				tip = "停发时间";
			}
		}
		tip_label.setText(tip);
	}

	private int SUBMIT = 0x00922;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == SUBMIT) {
				String status = msg.obj.toString();
				if ("0".equals(status)) {
					setResult(RESULT_OK);
					finish();
				} else if ("-1".equals(status)) {
					PopMsg.show("登录已过期,请重新登录");
					StartHelper.startActivity(Login.class);
					finish();
				} else if ("-2".equals(status)) {
					PopMsg.show("服务端异常");
				} else if ("-3".equals(status)) {
					PopMsg.show("输入参数无效");
				}
			}
		}

	};

	private void submitProcess(final String accepte_process_time) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				String wsdl_url = SelfApplication.WSDL_URL;// wsdl
				String namespace = SelfApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "processWorkOrderToReply2";// 要调用的方法名称
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", SelfApplication.getUserInfo().getTicket());
				request.addProperty("case_no", eomsInfo.getCase_no());
				request.addProperty("processTime", process_time.getText().toString() + "");
				request.addProperty("workOrderType", eomsInfo.getOrderType().index + "");
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						paserSubmit(envelope.getResponse().toString());
					}
				});
			}
		}).start();
	}

	private void paserSubmit(String stauts) {
		Message msg = new Message();
		msg.what = SUBMIT;
		msg.obj = stauts;
		handler.sendMessage(msg);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == process_time_ll.getId()) {
			java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			showDateTime(REQ_HITCH_TIME, df.format(new Date()));
		}
	}

	private void showDateTime(int requestCode, String currentTime) {
		WheelDateTimeDialog dateTimeDialog = new WheelDateTimeDialog(this, requestCode, currentTime);
		dateTimeDialog.setDateTimeChangeListener(new WheelDateTimeDialog.DateTimeChangeListener() {
			@Override
			public void onKeySave(int requestCode, String dateTimeValue) {
				if (REQ_HITCH_TIME == requestCode) {
					process_time.setText(dateTimeValue);
				}
			}
		});
		dateTimeDialog.show();
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.setTitle(R.string.work_order_reply);
		initTopBarReturn(topBar);
		topBar.setLogicBtnText("提交");
		topBar.setTopBarBtnListern(new TopBar.LogicBtnListern() {
			@Override
			public void onClickLogicBtn() {

				String accepte_process_time = process_time.getText().toString();

				if (StringUtils.isEmpety(accepte_process_time)) {
					PopMsg.show("请选择" + tip + "！");
					return;
				}
				submitProcess(accepte_process_time);
			}
		});

	}

}
